package com.gdpu.project.action.achievementManager;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.gdpu.page.vo.PageView;
import com.gdpu.page.vo.QueryResult;
import com.gdpu.project.service.ChengGuoService;
import com.gdpu.project.service.TeacherService;
import com.gdpu.project.util.DateUtil;
import com.gdpu.project.vo.CgInnovate;
import com.gdpu.project.vo.Teacher;
import com.opensymphony.xwork2.ActionSupport;

public class TechnologicalInnovationAction extends ActionSupport {

	private static final long serialVersionUID = 1L;
	private HttpServletRequest request;
	private HttpSession session;
	private int id;
	private String innovateId;
	private String teacherId;
	private String teacherName;
	private String innovateName;
	private String date;
	private String ranking;
	private String prizeLevel;
	private float keYanFen;
	private String comment;
	private String itemId;
	private String itemName;
	private TeacherService teacherService;
	private ChengGuoService chengguoService;
	private PageView<CgInnovate> pageView;
	private int currentpage=1;

	public PageView<CgInnovate> getPageView() {
		return pageView;
	}

	public void setCurrentpage(int currentpage) {
		this.currentpage = currentpage;
	}

	public void setChengguoService(ChengGuoService chengguoService) {
		this.chengguoService = chengguoService;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getInnovateId() {
		return innovateId;
	}

	public void setInnovateId(String innovateId) {
		this.innovateId = innovateId;
	}

	public String getTeacherId() {
		return teacherId;
	}

	public void setTeacherId(String teacherId) {
		this.teacherId = teacherId;
	}

	public String getTeacherName() {
		return teacherName;
	}

	public void setTeacherName(String teacherName) {
		this.teacherName = teacherName;
	}

	public String getInnovateName() {
		return innovateName;
	}

	public void setInnovateName(String innovateName) {
		this.innovateName = innovateName;
	}

	public String getDate() {
		return date;
	}

	public void setDate(String date) {
		this.date = date;
	}

	public String getRanking() {
		return ranking;
	}

	public void setRanking(String ranking) {
		this.ranking = ranking;
	}

	public String getPrizeLevel() {
		return prizeLevel;
	}

	public void setPrizeLevel(String prizeLevel) {
		this.prizeLevel = prizeLevel;
	}

	public float getKeYanFen() {
		return keYanFen;
	}

	public void setKeYanFen(float keYanFen) {
		this.keYanFen = keYanFen;
	}

	public String getComment() {
		return comment;
	}

	public void setComment(String comment) {
		this.comment = comment;
	}

	public void setItemId(String itemId) {
		this.itemId = itemId;
	}

	public String getItemId() {
		return itemId;
	}

	public void setItemName(String itemName) {
		this.itemName = itemName;
	}

	public String getItemName() {
		return itemName;
	}

	public void setTeacherService(TeacherService teacherService) {
		this.teacherService = teacherService;
	}

	// 增加指导学生科技创新活动
	public String addTechnologicalInnovation() throws Exception {
		request = ServletActionContext.getRequest();
		session = request.getSession();
		CgInnovate cgInnovate = new CgInnovate();
		cgInnovate.setComment(this.getComment());
		cgInnovate.setDate(DateUtil.stringToDate(this.getDate()));
		cgInnovate.setInnovateId(this.getInnovateId());
		cgInnovate.setInnovateName(this.getInnovateName());
		cgInnovate.setTeacherName(this.getTeacherName());
		cgInnovate.setKeYanFen(this.getKeYanFen());
		cgInnovate.setPrizeLevel(this.getPrizeLevel());
		cgInnovate.setRanking(this.getRanking());
		cgInnovate.setItemId(this.itemId);
		cgInnovate.setItemName(this.itemName);
		//set进对应的教师编号
		List<Teacher> teacherList=teacherService.findByName(teacherName);
		if(teacherList.size()>0){
			cgInnovate.setTeacherId(teacherList.get(0).getTeacherId());
		}			
		chengguoService.addCgInnovate(cgInnovate);
		List<CgInnovate> cgInnovatelist = findAllCgInnovateDepRole();				
		request.setAttribute("cgInnovatelist", cgInnovatelist);
		
		return SUCCESS;
	}


	

	// 查找所有指导学生科技创新活动
	public String findAllTechnologicalInnovation() throws Exception {
		request = ServletActionContext.getRequest();
		session = request.getSession();
		//角色判断，管理员返回所有，教职工返回相应的
		List<CgInnovate> cgInnovatelist = findAllCgInnovateDepRole();				
		request.setAttribute("cgInnovatelist", cgInnovatelist);
		return SUCCESS;
	}

	// 根据id指导学生科技创新活动
	public String findTechnologicalInnovationById() throws Exception {
		request = ServletActionContext.getRequest();
		session = request.getSession();
		CgInnovate cgInnovate = chengguoService.findCgInnovate(this.getId());
		request.setAttribute("cgInnovate", cgInnovate);
		return SUCCESS;
	}

	// 根据id删除指导学生科技创新活动
	public String delTechnologicalInnovationById() throws Exception {
		request = ServletActionContext.getRequest();
		session = request.getSession();
		chengguoService.deleteCgInnovateById(this.getId());
		//角色判断，管理员返回所有，教职工返回相应的
		List<CgInnovate> cgInnovatelist = findAllCgInnovateDepRole();				
		request.setAttribute("cgInnovatelist", cgInnovatelist);
		return SUCCESS;
	}

	
	/**
	 * 更新指导学生科技创新活动
	 */
	public String updateTechnologicalInnovation() throws Exception {
		request = ServletActionContext.getRequest();
		
		session = request.getSession();
		CgInnovate cgInnovate = this.chengguoService.findCgInnovate(this.getId());
		cgInnovate.setComment(this.getComment());
		cgInnovate.setDate(DateUtil.stringToDate(this.getDate()));
		cgInnovate.setInnovateId(this.getInnovateId());
		cgInnovate.setInnovateName(this.getInnovateName());
		cgInnovate.setTeacherName(this.teacherName);
		cgInnovate.setKeYanFen(this.getKeYanFen());
		cgInnovate.setPrizeLevel(this.getPrizeLevel());
		cgInnovate.setRanking(this.getRanking());
		cgInnovate.setItemId(this.itemId);
		cgInnovate.setItemName(this.itemName);	
		//set进对应的教师编号
		List<Teacher> teacherList=teacherService.findByName(teacherName);
		if(teacherList.size()>0){
			cgInnovate.setTeacherId(teacherList.get(0).getTeacherId());
		}		
		this.chengguoService.updateCgInnovate(cgInnovate);
		//角色判断，管理员返回所有，教职工返回相应的
		List<CgInnovate> cgInnovatelist = findAllCgInnovateDepRole();				
		request.setAttribute("cgInnovatelist", cgInnovatelist);		
		return SUCCESS;
	}
	
	
/**
 * 角色判断，管理员返回所有，教职工返回相应的
 * @return
 */
	private List<CgInnovate> findAllCgInnovateDepRole() {
		Teacher teacher=(Teacher)session.getAttribute("teacher");
		pageView=new PageView<CgInnovate>(10,currentpage);		
		QueryResult<CgInnovate> queryResult=null;
		if(teacher.getRole()==1){
		queryResult = this.chengguoService.findAllCgInnovate(pageView.getFirstResult(),pageView.getMaxresult());
		}else{
			queryResult = this.chengguoService.findAllCgInnovateByTeacherId(teacher.getTeacherId(),pageView.getFirstResult(),pageView.getMaxresult());
		}
		pageView.setQueryResult(queryResult);
		request.setAttribute("pageView", pageView);
		return queryResult.getResultlist();
	}
}
